package com.zt.ztzx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zt.ztzx.model.entity.device.DeviceGather;
import com.zt.ztzx.model.vo.DeviceVo;
import com.zt.ztzx.model.vo.common.Result;
import com.zt.ztzx.service.DeviceGatherService;
import com.zt.ztzx.mapper.DeviceGatherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* @author 16236
* @description 针对表【t_device_gather】的数据库操作Service实现
* @createDate 2024-09-05 09:27:19
*/
@Service
public class DeviceGatherServiceImpl extends ServiceImpl<DeviceGatherMapper, DeviceGather>
    implements DeviceGatherService{

    @Autowired
    private DeviceGatherMapper deviceGatherMapper;

    // 定时任务调度器
    private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

//    @Autowired
//    private RedisTemplate<String,List<DeviceVo>> redisTemplate;
    @Override
    public Result<PageInfo<DeviceVo>> getDeviceGatherList(Integer tid,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DeviceVo> list =  deviceGatherMapper.findAll();
        PageInfo<DeviceVo> pageInfo = new PageInfo<>(list);
        //每隔五分钟获取一次数据
        scheduler.schedule(this::getDeviceGatherListSaveRedis, 5, TimeUnit.MINUTES);
        return Result.build(pageInfo, 200, "查询成功");
    }

    @Override
    public Result removeData(Integer id) {
        this.removeById(id);
        return Result.build(null, 200, "删除成功");
    }

    @Override
    public Result updateData(DeviceGather deviceGather) {
        this.updateById(deviceGather);
        return Result.build(null, 200, "修改成功");
    }

    private void getDeviceGatherListSaveRedis() {
        List<DeviceVo> list = deviceGatherMapper.saveRedis();
        //保存到redis保存六十分钟
//        redisTemplate.opsForValue().set("deviceList",list,60, TimeUnit.MINUTES);
    }
}




